--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local CommandBase = require ".common.CommandBase"
require "common"
local AssertEx = AssertEx

local Command = class("Command2",CommandBase)
---------------------------------------------------------
-- Private
---------------------------------------------------------
function Command:ctor(message,machine)
	self.super:ctor(message,machine)
	-- self.msg = message 
	-- self.machine = machine 
	-- self.close  = false
	-- self.tableid = self.machine:getTableId()
	-- self.gameid = getGameIdbyTable(self.tableid)
	self:register()
end

--注册本服务里的消息
function Command:register()
	self.msg:registerSelf('enter_table_req',handler(self,self.enterTable))
	self.msg:registerSelf('leave_table_req',handler(self,self.leaveTable))	
	self.msg:registerSelf('pdk_status_req',handler(self,self.getStatus))
	self.msg:registerSelf('pdk_deal_req',handler(self,self.playDeal)) --出牌
	self.msg:registerSelf('pdk_tip_req',handler(self,self.getTip)) --提示
	self.msg:registerSelf('pdk_next_req',handler(self,self.playNext)) --不出
	self.msg:registerSelf('pdk_no_auto_req',handler(self,self.noAutoPlay)) --取消托管
	self.msg:registerSelf('pdk_ready_req',handler(self,self.ready)) --准备
	self.msg:registerSelf('pdk_jipai_req',handler(self,self.jipai)) --记牌

	self.msg:registerSelf('closeprivate_req',handler(self,self.closePrivate)) --关闭私人房
	self.msg:registerSelf('pdk_close_vote_req',handler(self,self.closeVote)) --投票

end




----------------------------------------------------------------------------------------
---游戏里的操作
------------------------------------------------------------------------------------------------
--进入桌子
function Command:enterTable(playerid,playerInfo)
	AssertEx(not self.close, 'enter_table_res' , 7) --游戏在维护
	AssertEx(playerid > 0 , 'enter_table_res' , 8) --玩家id错误
	local player = self.machine:getPlayer(playerid)
	if player then		
		if player.disconnect then --断线重连进来						
		else
			--没有断线重复请求进入桌子	
			log.debug(">>>>>没有断线重复请求进入桌子________",playerid,self.tableid)
		end
	else
		if not self.machine:canJoin(playerid) then 
			--人数已满
			AssertEx(false , 'enter_table_res' , 18)
		end
		--第一次进入
		player = table.copy(playerInfo)
		self.machine:addPlayer(player)	
		
	end
	player.disconnect = false
	self.machine:ntOnconnect(playerid)
	local result = {
		enter_table_res = {
			tableid = self.tableid,
			gameid = self.gameid,
		}	
	}
	return result
end

--离开
function Command:leaveTable(playerid)
	local player = self.machine:getPlayer(playerid)
	if player then												
		if self.machine:leaveTable(playerid) then 
			--房主不能离开桌子
			if playerid == self.machine.managerid then
				return {leave_table_res = GetErrorTable(1401)}
			end
			return {leave_table_res = {tableid = self.tableid}}
		end		
		--游戏已开始，请在休息时再退出！
		return {leave_table_res = GetErrorTable(9)}
	else
		--未进入房间
		return {leave_table_res = GetErrorTable(10)}		
	end
	return false
end

-- 得到桌子状态
function Command:getStatus(playerid,args)
	print("__getStatus_",playerid)
	local player = self.machine:getPlayer(playerid)		
	--无效操作
	AssertEx(player, 'pdk_status_res', 10)	
	if not self.machine:getPlayerSeatId(playerid) then 
		self.machine:sitDown(playerid)
	end
	if args.ready then 
		player.ready = true --准备
	end
	local status = self.machine:getTableStatus(playerid)
	return {
		pdk_status_res = status
	}
end

--提示
function Command:getTip(playerid)
	-- print("____getTip",playerid)
	local player = self.machine:getPlayer(playerid)
	AssertEx(player, 'pdk_tip_res', 10)	
	AssertEx(player.seatid>0, 'pdk_tip_res', 16)	
	local cards = self.machine:playTip(playerid)
	--没有取到提示牌
	AssertEx(cards, 'pdk_tip_res', 16)	
	return {
		pdk_tip_res = {
			cards = cards
		}
	}
end

--不出
function Command:playNext(playerid)
	local res = self.machine:playNext(playerid)
	--不出牌错误
	AssertEx(res, 'pdk_next_res', 16)		
	return {
		pdk_next_res = {
			succese = true
		}
	}	
end

--出牌
function Command:playDeal(playerid,args)
	local cards = args.cards
	local res = self.machine:playDeal(playerid,cards)
	--出牌错误
	AssertEx(res, 'pdk_deal_res', 16)		
	return {
		pdk_deal_res = {
			succese = true
		}
	}

end

--取消托管
function Command:noAutoPlay(playerid,args)
	local res = self.machine:noAutoPlay(playerid)
	AssertEx(res,'pdk_no_auto_res',16)
	return {
		pdk_no_auto_res = {
			succese = true
		}
	}
end

--准备
function Command:ready(playerid,args)	
	local res = self.machine:playReady(playerid)
	AssertEx(res,'pdk_ready_res',16)
	return {
		pdk_ready_res = {
			succese = true
		}
	}		
end

--记牌
function Command:jipai(playerid,args)	
	local res = self.machine:getOutCards(playerid)
	AssertEx(res,'pdk_jipai_res',16)	
	return {
		pdk_jipai_res = {
			-- rest_card = res,
			out_card = res,
		}
	}		
end

--
function Command:closePrivate(playerid)
	--没开局直接解散，开局后要投票
	if self.machine:closePrivate(playerid) then
		return true
	end
	return false
end


function Command:closeVote(playerid,args)
	print("__________closeVote__________",args)
	if self.machine:closeVote(playerid,args) then
		return true
	end
	return false
end



return Command